﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Controls;
using System.Windows.Threading;
using Clarendon.AzureBilling.Library;

namespace Clarendon.AzureBilling.Gui
{
    /// <summary>
    /// Class used to populate the hosted services list in the treeview
    /// </summary>
    public class PopulateHostedServices
    {
        /// <summary>
        /// An instance of the ServiceManager class
        /// </summary>
        private ServiceManager _manager = null;
        /// <summary>
        /// A semaphore used to lock out resources until the previous request has been made
        /// TODO: Replace this semaphore with a request queue so that each will execute sequentially when the previous one has returned
        /// </summary>
        private Semaphore _semaphore = null;
        /// <summary>
        /// A handle to the main window so that thread updates can be done to the UI safely
        /// </summary>
        private MainWindow _window = null;

        /// <summary>
        /// Default constructor to create the various objects needed to populate the list
        /// </summary>
        public PopulateHostedServices()
        {
            _semaphore = new Semaphore(0, 3);
            _manager = new ServiceManager(_semaphore);
            _window = (MainWindow) App.Current.MainWindow;
        }

        /// <summary>
        /// Builds the service tree list of nodes containing all of the hosted services
        /// </summary>
        public void BuildServiceTreeNodes()
        {
            // make the service request
            ServiceListRequester requester = new ServiceListRequester(_manager);
            requester.MakeServiceRequest();
            // block the current thread
            _semaphore.WaitOne();
            // make the request for the properties of the service
            ServicePropertiesRequester properties = new ServicePropertiesRequester(_manager);
            properties.AzureHostedServiceListReturned +=
                new AzureHostedServiceEventHandler(PropertiesAzureHostedServiceListReturned);
            properties.MakeServiceRequest();
        }

        /// <summary>
        /// Callback method for the event which will safely setup the treeview control
        /// </summary>
        private void PropertiesAzureHostedServiceListReturned(AzureResponseEventArgs args)
        {
            // make sure that the update to the UI is thread safe           
            _window.Dispatcher.BeginInvoke(
                (Action)(() =>
                {
                    List<HostedService> services = args.HostedServices;
                    // enumerate the hosted services and then add them one by one to the treeview
                    foreach (var hostedService in services)
                    {
                        var item = new TreeViewItem
                        {
                            Name = hostedService.Name,
                            Header =
                                String.Format("{0} ({1})", hostedService.Name,
                                              hostedService.Location)
                        };
                        var itemServices = (TreeViewItem)_window.tvHostedServices.Items.GetItemAt(0);
                        itemServices.Items.Add(item);
                    }
                }));
        }
    }
}
